package com.fangcun.common.auth;

import org.springframework.util.StringUtils;

import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.encryption.AESUtils;

import lombok.Data;

/**
 * @作者： 陳曉鬆 @时间： 2020年4月27日 - 下午12:45:53 @description：->
 */
public class UserAuthMethod {

	/** 用户登录header token参数名称 **/
	public static final String LOGIN_TOKEN_NAME = "token";

	/** 用户存储redis db **/
	public static final int LOGIN_REDIS_DB = 1;

	/** 用户存储redis 时间 **/
	public static final int LOGIN_REDIS_TIME = 24 * 60 * 60 * 30;// 未操作，需要从新登录

	/** 用户信息redis存储 Aeskey **/
	public static final String LOGIN_AES_KEY = "shuying12345!@#0";

	/** 获取存储到redis的用户信息的key **/
	public static String getLoginRedisKey(String code, Long userId) {
		return "FANGCUN|LOGIN|" + code + "_" + userId;
	}
	/** 微信登录 未绑定手机号 记录帐号 **/
	public static String getBindPhoneRedisKey(String appId, String openId) {
		return "FANGCUN|BIND|" + appId.toUpperCase() + "_" + openId.toUpperCase();
	}
	/** 获取前端验证token **/
	public static String getLoginToken(String key, Long lastLoginTime) {
		try {
			return AESUtils.encrypt(key + "_" + lastLoginTime, LOGIN_AES_KEY);
		} catch (Exception e) {
			return ErrCode.ERR.getReturnMsg();
		}
	}

	/** 获取前端登录token解析的实体 **/
	public static LoginRedisKey getLoginRedisKeyClass(String token) throws ProException {
		if (StringUtils.isEmpty(token)) {
			throw new ProValiDataException(ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnMsg());
		}
		// 解密
		String key = null;
		try {
			key = AESUtils.decrypt(token, UserAuthMethod.LOGIN_AES_KEY);
		} catch (Exception e) {
			throw new ProValiDataException(ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnMsg() + ":异常");
		}
		if (StringUtils.isEmpty(key)) {
			throw new ProValiDataException(ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnMsg());
		}
		if (!key.contains("_")) {
			throw new ProValiDataException(ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnCode(), ErrCode.USER_LOGIN_TOKEN_FAIL.getReturnMsg());
		}
		String str[] = key.split("_");
		LoginRedisKey loginRedisKey = new UserAuthMethod().new LoginRedisKey();
		try {
			loginRedisKey.setRedisKey(key.substring(0, key.lastIndexOf("_")));
			loginRedisKey.setUserId(Long.valueOf(str[1]));
			if (str.length > 3) {
				loginRedisKey.setAccount(str[2]);
				loginRedisKey.setLastLoginTime(Long.valueOf(str[3]));
			} else {
				loginRedisKey.setLastLoginTime(Long.valueOf(str[2]));
			}
		} catch (Exception e) {
			throw new ProValiDataException(ErrCode.USER_LOGIN_BE_OVERDUE.getReturnCode(), ErrCode.USER_LOGIN_BE_OVERDUE.getReturnMsg());
		}
		return loginRedisKey;
	}

	/** 前端登录token解析的实体 **/
	@Data
	public class LoginRedisKey {
		private String redisKey;
		private Long lastLoginTime;
		private Long teahouseId;
		private Long userId;
		private String account;
	}
}
